﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics;
using System.Data;

namespace XEvents.Library.Targets
{
    public class TargetDataFactory
    {
        internal static TargetData GetTargetData(EventSessionTarget target)
        {
            string data = target.parent.Server.ServerConnection.ExecuteScalar(string.Format(SqlCommandStrings.TargetData, target.parent.Name, target.Name.Split('.').ElementAt(1))).ToString();

            switch (target.Name.Split('.').ElementAt(1))
            {
                case "ring_buffer":
                    return Target_RingBuffer.GetTargetData(target.parent.Server, target);
                case "synchronous_event_counter":
                    return Target_Counter.GetTargetData(data, target.parent.Server, target);
                case "asynchronous_file_target":
                    return Target_File.GetTargetData(target.parent.Server, target);

                    /*
					// Fix for id# 7948 Invalid File Path used in Target Dataviewer
                    //target.Options["filename"].Value.Substring(0, .Split('.').ElementAt(0)+"*."+target.Options["filename"].Value.Split('.').ElementAt(1);
                    //string metaname = target.Options["metadatafile"].Value.Split('.').ElementAt(0) + "*." + target.Options["metadatafile"].Value.Split('.').ElementAt(1);

                    string file = target.Options["filename"].Value;
                    string meta;

                    if (target.Options.Where(p => p.Name =="metadatafile").Count() > 0)
                        meta = target.Options["metadatafile"].Value;
                    else
                        meta = file.Substring(0, file.LastIndexOf(".")) + ".XEM";

                    string filename = file.Substring(0, file.LastIndexOf(".")) + "*" + file.Substring(file.LastIndexOf("."));
                    string metaname = meta.Substring(0, meta.LastIndexOf(".")) + "*" + meta.Substring(meta.LastIndexOf("."));

                    string cmd = string.Format(SqlCommandStrings.FileTargetData, filename, metaname);
                    Debug.WriteLine(cmd);
                    return Target_File.GetTargetData(target.parent.Server.ServerConnection.ExecuteReader(cmd), data, target.parent.Server, target);
                    */
                case "pair_matching":
                    return Target_PairMatching.GetTargetData(data, target.Parent.Server, target);
                case "asynchronous_bucketizer":
                    return Target_Bucketizer.GetTargetData(data, target.Parent.Server, target);
                default:
                    return null;
            }
        }
        public static DataTable GetTargetDataTable(TargetData data)
        {
            DataTable dt = new DataTable();
            if (data is Target_RingBuffer)
            {
                Target_RingBuffer tr = ((Target_RingBuffer)data);
                dt = tr.GetEventsAsDataTable();
            }
            else if (data is Target_File)
            {
                Target_File tf = ((Target_File)data);
                dt = tf.GetEventsAsDataTable();
            }
            else if (data is Target_PairMatching)
            {
                Target_PairMatching tp = ((Target_PairMatching)data);
                dt = tp.GetEventsAsDataTable();
            }
            else if (data is Target_Counter)
            {
                Target_Counter tc = ((Target_Counter)data);
                dt = tc.GetEventsAsDataTable();
            }
            return dt;
        }
    }
}
